#pragma once
#include "uart/ProtocolSender.h"
#include "utils/BrightnessHelper.h"
#include <chrono>
#include <ctime>
#include "utils/TimeHelper.h"
#include <ntp/ntp.h>
#include "logic_utility.hpp"
#include "manager/hardware.h"
#include "net/NetManager.h"




static bool StatusFalg=false;

static bool NavibarFalg = false;

static bool IsClose=false;
// 更新星期
    const char *weekdays[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
static void updateUI_time() {

	struct tm *t = TimeHelper::getDateTime();
	char buf[1024]={0};
	snprintf(buf,sizeof(buf),"%d",t->tm_year+1900);
	myearPtr->setText(buf);
	snprintf(buf,sizeof(buf),"%02d",t->tm_mon+1);
	mmonthPtr->setText(buf);
	snprintf(buf,sizeof(buf),"%02d",t->tm_mday);
	mdayPtr->setText(buf);
    if(t->tm_hour < 12){
    	mhalfAdayPtr->setText("上午");
    }
    else{
    	mhalfAdayPtr->setText("下午");
    }

    mweekPtr->setText(weekdays[t->tm_wday]);
}

static TouchClass touchlock;
class SlidelockSeekBarChangeListener : public ZKSeekBar::ISeekBarChangeListener {
public:
	virtual void onProgressChanged(ZKSeekBar *pSeekBar, int progress) {
//		LOGD("progress=%d\n",progress);
		if(progress !=100){
			IsClose=true;
		}
	}
	virtual void onStopTrackingTouch(ZKSeekBar *pSeekBar) {
		if (pSeekBar->getProgress() == 100 && IsClose) {
			operateLock(false);
			//蜂鸣器
		    hw::beep();
		} else {
			pSeekBar->setProgress(0);
			touchlock.setProgressByPercentage(0);
			IsClose=false;
		}
	}
	virtual void onStartTrackingTouch(ZKSeekBar *pSeekBar) {
//		LOGD("pSeekBar->getProgress()=%d\n",pSeekBar->getProgress());

	}

};
static SlidelockSeekBarChangeListener sSlidelockSeekBarChangeListener;


/**
 * 注册定时器
 * 填充数组用于注册定时器
 * 注意：id不能重复
 */
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
	//{0,  6000}, //定时器id=0, 时间间隔6秒
	{1,  1000},
};


/**
 * 当界面构造时触发
 */
static void onUI_init(){
	//关闭键盘
	EASYUICONTEXT->hideIME();
	touchlock.setValues(mlockTextPtr, mlockTextPtr->getPosition(), 590);
	//注册监听滑块
	mslidelockSeekBarPtr->setTouchListener(&touchlock);
	mslidelockSeekBarPtr->setSeekBarChangeListener(&sSlidelockSeekBarChangeListener);

	mTextView1Ptr->setTouchPass(true);
	mTextView3Ptr->setTouchPass(true);
	mTextView4Ptr->setTouchPass(true);
//	EASYUICONTEXT->setScreensaverEnable(true);
//	EASYUICONTEXT->setScreensaverTimeOut(60);

	if(EASYUICONTEXT->isStatusBarShow()){
		StatusFalg=true;
	}
	if(EASYUICONTEXT->isNaviBarShow()){
		NavibarFalg = true;
		EASYUICONTEXT->hideNaviBar();
	}

	updateUI_time();

	IsClose = false;
	Global::Water::Count = 0;
	LOGD("--------------lock_init---------------------");

}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {
	EASYUICONTEXT->hideStatusBar();
}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {

}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {
	mslidelockSeekBarPtr->setSeekBarChangeListener(NULL);

	if(StatusFalg){
		StatusFalg=false;
		EASYUICONTEXT->showStatusBar();
	}
	if(NavibarFalg){
		NavibarFalg = false;
		EASYUICONTEXT->showNaviBar();
	}
}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {

}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
static bool onUI_Timer(int id){
	switch (id) {
	case 1:{
		updateUI_time();
	}break;
		default:
			break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool onlockActivityTouchEvent(const MotionEvent &ev) {
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			break;
		default:
			break;
	}
	return false;
}
static void onProgressChanged_slidelockSeekBar(ZKSeekBar *pSeekBar, int progress) {
    //LOGD(" ProgressChanged slidelockSeekBar %d !!!\n", progress);
}
static bool onButtonClick_Button1(ZKButton *pButton) {
    LOGD(" ButtonClick Button1 !!!\n");
    shot();
    return false;
}
